# -*- coding: utf-8 -*-
from email.MIMEMultipart import MIMEMultipart 
from email.MIMEBase import MIMEBase 
from email.MIMEText import MIMEText 
from email import Encoders

import os
import smtplib
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.MIMEAudio import MIMEAudio
from email.MIMEImage import MIMEImage
from email.Encoders import encode_base64
from email.utils import COMMASPACE
import email


class SendEmail():
    def __init__(self):
        self.name = ""
    def Send(self, username, password, fromemail, recipientemail, ccemail, bccemail, title, message, attachname, attachcontent):
        recipient = []
        cc = []
        recipient.append(recipientemail)
        cc.append(ccemail)
        mailServer = smtplib.SMTP("smtp.exmail.qq.com", 587)
        mailServer.ehlo()
        mailServer.starttls()
        mailServer.ehlo()
        mailServer.login(username, password)

        msg = MIMEMultipart()
        msg['From'] = fromemail
        msg['To'] = COMMASPACE.join(recipient)
        msg['Cc'] = COMMASPACE.join(cc)
        msg['Subject'] = title
        msg.attach(MIMEText(message))

        if os.path.exists(attachname):
            part = MIMEBase('application', 'octet-stream')
            part.set_payload(open(attachname, 'rb').read())
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition',
                'attachment; filename="%s"' % os.path.basename(attachname))
            msg.attach(part)
        # msg.attach(self.getAttachment(attachname))
        print msg['To']
        mailServer.sendmail(fromemail, recipient+cc, msg.as_string())
        # Should be mailServer.quit(), but that crashes...
        mailServer.close()

    def getAttachment(self, attachmentFilePath):
        contentType, encoding = mimetypes.guess_type(attachmentFilePath)

        if contentType is None or encoding is not None:
            contentType = 'application/octet-stream'

        mainType, subType = contentType.split('/', 1)
        file = open(attachmentFilePath, 'rb')

        if mainType == 'text':
            attachment = MIMEText(file.read())
        elif mainType == 'message':
            attachment = email.message_from_file(file)
        elif mainType == 'image':
            attachment = MIMEImage(file.read(),_subType=subType)
        elif mainType == 'audio':
            attachment = MIMEAudio(file.read(),_subType=subType)
        else:
            attachment = MIMEBase(mainType, subType)
        attachment.set_payload(file.read())
        encode_base64(attachment)

        file.close()

        attachment.add_header('Content-Disposition', 'attachment',   filename=os.path.basename(attachmentFilePath))
        return attachment




if __name__ == "__main__":
    sendemail = SendEmail()
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
    the_file_name = BASE_DIR + "/upload/"
    attachname = the_file_name + "a.txt"
    sendemail.Send("chengk@chinaetf.com.cn", "ktzx123456",
                   "chengk@chinaetf.com.cn", "575393442@qq.com", "1766266818@qq.com",
                   "", "Hello from python!", "This is a email sent with python", attachname, "")